package com.xuanyang.dormrepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.model.entity.Notice;
import com.xuanyang.dormrepair.service.NoticeService;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR soft
 * @DATE 2019/5/23 0:44
 * @DESCRIBE 公告表
 */
@AdminLogin
@RestController
@RequestMapping("/admin/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @PostMapping("/page")
    public RestResponse page(int page, int limit, Notice query) {
        IPage<Notice> iPage = noticeService.page(page, limit, query);

        if (iPage.getTotal()>0) {
            return RestResponse.ok(iPage);
        }
        return RestResponse.fail();
    }

    @PostMapping("/add")
    public RestResponse add(Notice notice) {
        if (noticeService.add(notice)) {
            return RestResponse.ok();
        }

        return RestResponse.fail();
    }

    @PostMapping("/del/{id}")
    public RestResponse del(@PathVariable Integer id) {
        if (noticeService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/change")
    public RestResponse change(Notice notice) {
        if (noticeService.update(notice)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
